//注意：首先在页面中引入两个js，再引入以下js
//<script src="https://cdn.bootcss.com/jquery/1.12.0/jquery.min.js"></script>
//<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>


// *****************************************微信分享**************************************
//开发文档：https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115
//生成签名后的初始化
var initConfig= function(data){
    wx.config({
        debug: false,
        appId: 'wx48b417fb6db0844f',
        timestamp: data.timestamp,
        nonceStr: data.nonceStr,
        signature: data.signature,
        //枚举需要用到的接口
        jsApiList: ['onMenuShareAppMessage', 'onMenuShareTimeline','onMenuShareQQ','onMenuShareQZone','onMenuShareWeibo']
    });
}

// --------------------------前端生成签名，此种方式级不安全
//生成签名
var calcSignature =function(ticket, nonceStr, timeStamp, url) {
    var result = {
        jsapi_ticket: ticket,
        nonceStr: nonceStr,
        timestamp: timeStamp,
        url: url,
    }
    var str = 'jsapi_ticket=' + ticket + '&noncestr=' + nonceStr + '&timestamp=' + timeStamp + '&url=' + url;
    // 对str使用sha1签名，得到signature，这里使用jsSHA模块，需install
    shaObj = new jsSHA(str, 'TEXT');
    result.signature = shaObj.getHash('SHA-1', 'HEX');
    initConfig(result);
    return result; // 返回到前端，提供接口由前端请求
}
// 随机字符串产生函数
var nonceStr = function() {
    return Math.random().toString(36).substr(2, 15);
};
// 时间戳产生函数
var timeStamp = function () {
    return parseInt(new Date().getTime() / 1000) + '';
};
// 获取签名所需ticket,调用生成签名
$.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx48b417fb6db0844f&secret=28bc14e8cfd92cd858a3094ff34f8902", function(data){
    $.get("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+ data.access_token, function(data){
        calcSignature(data.jsapi_ticket, nonceStr(), timeStamp(), location.href);
    })
})



//--------------------------------从后台获取签名信息,推荐
//后端代码案例 ： https://github.com/arronf2e/jssdk_simple
/*$.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx48b417fb6db0844f&secret=28bc14e8cfd92cd858a3094ff34f8902", function(data){
    $.get("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+ data.access_token, function(data){
        $.post("生成签名的服务端url", {jsapi_ticket: data.jsapi_ticket, url: location.href}, function(data){
            initConfig(data);
        })
    })
})*/




//--------------------------------前端分享代码

var shareData= {
    title: '一言不合就发红包，金蛛送您200元红包！',
    desc: '一言不合就发红包，金蛛送您200元红包！',
    link: location.href,
    imgUrl: 'http://'+location.host+'/images/icon-200yuan.png',
    type: 'link', //分享类型，music/video/link,默认为link
    dataUrl: '', //如果type为music/video,则要提供数据链接，默认为空
    success: function() {
        //分享成功
        // alert('share success');
    },
    cancel: function() {
        //用户手动取消分享
    }
};
function configWeChatInterface() {
    //分享给朋友
    wx.onMenuShareAppMessage(shareData);
    //分享到朋友圈
    wx.onMenuShareTimeline(shareData);
    //分享到QQ
    wx.onMenuShareQQ(shareData);
    //分享到QQ空间
    wx.onMenuShareQZone(shareData);
    //分享到微博
    wx.onMenuShareWeibo(shareData);
    //分享失败
    wx.error(function(res){
        alert("分享失败");
    })
};